﻿
function extraInit()
{
    initTree(false, false, false);
}

function selectMonth()
{
    var month = parseInt($("input[name=tbxMonth]").val());
    var year = parseInt($("input[name=tbxYear]").val());
    var day = parseInt($("input[name=tbxDay]").val());
    if (isNaN(month) || isNaN(year))
    {
        var date = new Date();
        month = date.getMonth();
        year = date.getFullYear();
        day = date.getDay();
    }
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function previousMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month--;
    if (month < 1)
    {
        year--;
        month = 12;
    }
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function nextMonth()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    month++;
    if (month > 12)
    {
        year++;
        month = 1;
    }
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function previousDay()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    var date = new Date();
    date.setFullYear(year);
    date.setMonth(month-1);
    date.setDate(day-1);
    viewWorktimeSheet(jobTitleId, date.getFullYear(), date.getMonth()+1, date.getDate(), 1);
}

function nextDay()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    var date = new Date();
    date.setFullYear(year);
    date.setMonth(month-1);
    date.setDate(day+1);
    viewWorktimeSheet(jobTitleId, date.getFullYear(), date.getMonth()+1, date.getDate(), 1);
}

function previousYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year--;
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function nextYear()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    year++;
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function selectTreeNode(jobTitleId)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    viewWorktimeSheet(jobTitleId, year, month, day, 1);
}

function selectPage(index)
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());
    var jobTitleId = $("a.jstree-clicked").closest("li").attr("id");
    viewWorktimeSheet(jobTitleId, year, month, day, index);
}

function viewWorktimeSheet(jobTitleId, year, month, day, pageIndex)
{
    $("input[name=tbxMonth]").val(month);
    $("input[name=tbxYear]").val(year);
    $("input[name=tbxDay]").val(day);
    $("#hdfMonth").val(month);
    $("#hdfYear").val(year);
    $("#hdfDay").val(day);
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Worktime/AjaxWorktime.aspx",
	    data : { 
		    "action" : "get_daily_worktime_sheet", 
		    "jobTitleId" : jobTitleId,
		    "year" : year,
		    "month" : month,
		    "day" : day,
		    "pageIndex" : pageIndex
	    }, 
	    success : function (r) {
	        if (r.status)
			{
			    var table = $("#worktimeTable");
			    var rows = table.find(".div-row");
			    for (var i=rows.length-1; i>0; i--)
			    {
			        rows.eq(i).remove();
			    }
			    for (var i=0; i<r.list.length; i++)
			    {
			        var htmlRow = "<div class='div-row'>";
                    htmlRow += "<div class='div-cell c0'>";
                    htmlRow += r.list[i].order;
                    htmlRow += "</div>";
                    htmlRow += "<div class='div-cell c1'>";
                    htmlRow += r.list[i].fullName;
                    htmlRow += "<input type='hidden' name='hdfAgentId' value='"+r.list[i].agentId+"'/>";
                    htmlRow += "</div>";
                    var morningLeaveHasApp = "";
                    var morningLeaveNoApp = "";
                    if (r.list[i].leaveMorning == 1)
                    {
                        morningLeaveHasApp = "checked='checked'";
                    }
                    else if (r.list[i].leaveMorning == 2)
                    {
                        morningLeaveNoApp = "checked='checked'";
                    }
                    var name = randomString();
                    htmlRow += "<div class='div-cell c5' style='text-align:center'><input type='checkbox' name='ckbMorning-"+name+"' value='1' "+morningLeaveHasApp+" onclick='setCheck(this)'/></div>";
                    htmlRow += "<div class='div-cell c5' style='text-align:center'><input type='checkbox' name='ckbMorning-"+name+"' value='2' "+morningLeaveNoApp+" onclick='setCheck(this)'/></div>";
                    
                    var afternoonLeaveHasApp = "";
                    var afternoonLeaveNoApp = "";
                    if (r.list[i].leaveAfternoon == 1)
                    {
                        afternoonLeaveHasApp = "checked='checked'";
                    }
                    else if (r.list[i].leaveAfternoon == 2)
                    {
                        afternoonLeaveNoApp = "checked='checked'";
                    }
                    name = randomString();
                    htmlRow += "<div class='div-cell c5' style='text-align:center'><input type='checkbox' name='ckbAfternoon-"+name+"' value='1' "+afternoonLeaveHasApp+" onclick='setCheck(this)'/></div>";
                    htmlRow += "<div class='div-cell c5' style='text-align:center'><input type='checkbox' name='ckbAfternoon-"+name+"' value='2' "+afternoonLeaveNoApp+" onclick='setCheck(this)'/></div>";
                    
                    htmlRow += "</div>";
                    table.append(htmlRow);
			    }
			    
			    var divPaging = $("#divPaging");
			    var paging = "";
			    for (var i=1; i<=r.totalPage; i++)
			    {
			        if (r.pageIndex == i)
			        {
			            paging += "<span>"+i+"</span>";
			        }
			        else
			        {
			            paging += "<a href='javascript:void(0);' onclick='selectPage("+i+");'>"+i+"</a>";
			        }
			    }
			    divPaging.html(paging);
			}
	    }
    })
    .always(function() {
	    unmask(area);
	});
}

function setCheck(ckb)
{
    var jCkb = $(ckb);
    var ckbs = $("input[type=checkbox][name="+jCkb.attr("name")+"]");
    if (jCkb.attr("checked") == "checked")
    {
        for (var i=0; i<ckbs.length; i++)
        {
            if (ckbs.eq(i).val() != jCkb.val())
            {
                ckbs.eq(i).removeAttr("checked");
            }
        }
    }
}

function exportExcel()
{
    var li = $("a.jstree-clicked").closest("li");
    if (li.length  == 0)
    {
        return;
    }
    $("#hdfJobTitleId").val(li.attr("id"));
}

function updateWorktime()
{
    var month = parseInt($("#hdfMonth").val());
    var year = parseInt($("#hdfYear").val());
    var day = parseInt($("#hdfDay").val());

    var table = $("#worktimeTable");
    var rows = table.find(".div-row");
    if (rows.length < 2)
    {
        return;
    }
    
    var postData = "";
    for (var i=1; i<rows.length; i++)
    {
        var agentId = rows.eq(i).find("input[name=hdfAgentId]").val();
    
        var morning = 0;
        var ckbMorning = rows.eq(i).find("input:checked[name^=ckbMorning]");
        if (ckbMorning.length > 0)
        {
            morning = ckbMorning.eq(0).val();
        }
        
        var afternoon = 0;
        var ckbAfternoon = rows.eq(i).find("input:checked[name^=ckbAfternoon]");
        if (ckbAfternoon.length > 0)
        {
            afternoon = ckbAfternoon.eq(0).val();
        }
        
        if (postData != "")
        {
            postData += ",";
        }
        postData += agentId + "|" + morning + "|" + afternoon;
    }
    
    var area = mask();
    $.ajax({
	    async : false,
	    type: 'POST',
	    url: "/Worktime/AjaxWorktime.aspx",
	    data : { 
		    "action" : "update_daily_worktime_sheet", 
		    "year" : year,
		    "month" : month,
		    "day" : day,
		    "postData" : postData
	    }, 
	    success : function (r) {
	        if (r.status)
			{
                
            }
            else
            {
                if (r.error != "")
                {
                    alert(r.error);
                }
            }
        }
    })
    .always(function() {
	    unmask(area);
	});
}



